package mac5789.utils;

import java.util.Iterator;

public class Range implements Iterable<Integer> {

    private final int from;

    private final int to;

    private final int step;

    public static Range range(int from, int to) {
        return new Range(from, to);
    }

    public Range(int from, int to) {
        this(from, to, 1);
    }

    public Range(int from, int to, int step) {
        this.from = from;
        this.to = to;
        this.step = step;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new RangeIterator(this.from, this.to, this.step);
    }

    public static class RangeIterator implements Iterator<Integer> {
        private int current;

        private final int to;

        private final int step;

        private RangeIterator(int from, int to, int step) {
            this.current = from;
            this.to = to;
            this.step = step;
        }

        @Override
        public Integer next() {
            Integer next = this.current;
            this.current += this.step;
            return next;
        }

        @Override
        public boolean hasNext() {
            return this.current + this.step <= this.to;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
}
